<template>
  <div class="searchpage">
    <input
      type="text"
      v-model="text"
      class="search"
      placeholder="请输入关键字"
    />
    <i class="icon"></i>
    <div v-if="!text">
      <span
        v-for="(item, index) in keywordList"
        :key="index"
        class="keyword"
        @click="fill(item.first)"
        >{{ item.first }}</span
      >
    </div>
    <div v-else>
      <ul>
        <li v-for="(item, index) in songList" :key="index">{{ item.name }}</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: "",
      keywordList: [],
      // 热搜词是否显示
      isHotShow: false,
      songList: [],
    };
  },
  created() {
    this.getHotData();
  },
  methods: {
    // 获取热搜词
    getHotData() {
      this.$http
        .get("/api/search/hot")
        .then((res) => {
          this.keywordList = res.data.result.hots;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 填空
    fill(keyword) {
      this.text = keyword;
      // 隐藏热搜词
      this.isHotShow = false;
      this.$http("/api/search?keywords=" + keyword)
        .then((res) => {
          this.songList = res.data.result.songs;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style scoped>
.search {
  display: block;
  width: 80%;
  height: 32px;
  margin: 10px auto;
  padding-left: 40px;
  font-size: 16px;
  background-color: #f4f4f4;
  outline: none;
  border: none;
  border-radius: 16px;
}
.searchpage {
  position: relative;
}
.icon {
  position: absolute;
  top: 8px;
  left: 28px;
  display: block;
  width: 18px;
  height: 18px;
  background-image: url("../../static/img/search.png");
  background-size: contain;
}
.keyword {
  display: inline-block;
  height: 32px;
  line-height: 32px;
  border: 1px solid #666;
  border-radius: 16px;
  padding: 0 10px;
  margin: 10px 16px;
}
</style>